package org.babygo.connect;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.babygo.connect.client.NettyClient;
import org.babygo.connect.server.NettyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.babygo.transport.api.FileClientOperator;
import com.babygo.transport.api.FileServerOperator;
import com.babygo.view.api.ViewOperator;

public class NettyFileOperator implements FileServerOperator, FileClientOperator{
	private Logger log = LoggerFactory.getLogger(NettyFileOperator.class);

	public static ViewOperator OPERTATOR;
	public static Map<String, File> needToSavedFiles = new HashMap<String, File>();//准备保存的文件
	public static Map<String, File> readyToSendFiles = new HashMap<String, File>();//准备发送的文件

	private NettyService nettyService = new NettyService(9981);

	private NettyClient nettyClient = new NettyClient();

	public NettyService getNettyService() {
		return nettyService;
	}

	public NettyClient getNettyClient() {
		return nettyClient;
	}

	public boolean start(int port) {
		OPERTATOR.writeLog("[服务端]准备在" + port + "端口开启服务!");
		return nettyService.run();
	}

	public boolean connect(String ip, int port) {
		log.info(String.format("准备连接到:%s-%s", ip, port));
		return nettyClient.connect(ip, port);
	}

	public void close() {
	}

	public void closeClient() {
		nettyClient.close();
	}

	public void closeServer() {
		nettyService.stop();
	}

	public static void setViewOperator(ViewOperator operator) {
		OPERTATOR = operator;
	}

	public boolean sendFile(String ip, String port, File f) {
		nettyService.sendFile(ip, port, f);
		return false;
	}

	public NettyService getService() {
		return nettyService;
	}
}